// ==UserScript==
// @name         Bweb
// @namespace    http://tampermonkey.net/
// @version      v1.0.20241121
// @description  try to take over the world!
// @author       You
// @match        https://www.bilibili.com/*
// @match        bilibili.com/*
// @match        www.bilibili.com/*
// @icon         
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  let cacheName = 'myBliBliCache_v3'

  setTimeout(() => {
    initialize()
    addGlobalStyle()
  }, 100)

  function addGlobalStyle() {
    document.body.appendChild(document.createElement('style')).innerHTML = `.ad-report:not(:empty){display: none!important;}.activity-m-v1{display:none!important;}.pop-live-small-mode{display:none!important;}`
  }

  // 定义全局变量
  let myBliBliData = [];

  // 初始化数据
  function initializeData() {
    const cachedData = localStorage.getItem(cacheName);
    if (cachedData) {
      myBliBliData = JSON.parse(cachedData);
    } else {
      // 默认数据
      myBliBliData = [
        {
          selector: '#nav-top-section',
          isToggle: false,
          name: '导航',
          controlElList: ['.left-entry li']
        },
        {
          selector: '#comment-section',
          isToggle: false,
          name: '评论区',
          controlElList: ['#commentapp', '.activity-m-v1']
        },
        {
          selector: '#sidebar-menu',
          isToggle: false,
          name: '右侧',
          controlElList: ['#danmukuBox', '#slide_ad', '.recommend-list-v1', '.ad-report']
        }
      ];
      localStorage.setItem(cacheName, JSON.stringify(myBliBliData));
    }

    // 初始化时应用所有显示/隐藏状态
    applyToggleStates();
  }

  // 切换元素显示状态
  function toggleElement(elementId) {
    const elements = document.querySelectorAll(elementId);
    if (elements.length) {
      elements.forEach(element => {
        element.style.display = element.style.display === 'none' ? 'block' : 'none';
      })
    }
  }

  // 应用所有元素的显示状态
  function applyToggleStates() {
    myBliBliData.forEach(item => {
      item.controlElList.forEach(elementId => {
        const elements = document.querySelectorAll(elementId);
        if (elements.length) {
          elements.forEach(element => {
            element.style.display = item.isToggle ? 'block' : 'none';
          })
        }
      });
    });
  }

  // 处理按钮点击事件
  function handleButtonClick(index) {
    myBliBliData[index].isToggle = !myBliBliData[index].isToggle;
    localStorage.setItem(cacheName, JSON.stringify(myBliBliData));
    console.log(myBliBliData[index])
    // 更新按钮状态
    updateButtonStyles();

    // 切换相关元素的显示状态
    myBliBliData[index].controlElList.forEach(elementId => {
      toggleElement(elementId);
    });
  }

  // 创建侧边栏
  function createSidebar() {
    const sidebar = document.createElement('div');
    sidebar.style.cssText = `
        position: fixed;
        left: 0;
        top: 120px;
        background: linear-gradient(135deg, #007bff, #0056b3);
        padding: 15px;
        border-radius: 0 15px 15px 0;
        box-shadow: 2px 2px 10px rgba(0,0,0,0.2);
        z-index: 9999;
        display: flex;
        flex-direction: column;
        gap: 10px;
    `;

    myBliBliData.forEach((item, index) => {
      const button = document.createElement('button');
      button.textContent = item.name;
      button.className = 'toggle-button';
      button.style.cssText = `
            padding: 8px 15px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: all 0.3s ease;
            background-color: ${item.isToggle ? '#ffffff' : 'transparent'};
            color: ${item.isToggle ? '#007bff' : '#ffffff'};
            font-weight: bold;
            min-width: 100px;
            text-align: center;
            margin: 2px 0;
        `;

      button.addEventListener('click', () => handleButtonClick(index));
      button.addEventListener('mouseover', () => {
        button.style.transform = 'scale(1.05)';
      });
      button.addEventListener('mouseout', () => {
        button.style.transform = 'scale(1)';
      });

      sidebar.appendChild(button);
    });

    return sidebar;
  }

  // 更新按钮样式
  function updateButtonStyles() {
    const buttons = document.querySelectorAll('.toggle-button');
    buttons.forEach((button, index) => {
      const isToggled = myBliBliData[index].isToggle;
      button.style.backgroundColor = isToggled ? '#ffffff' : 'transparent';
      button.style.color = isToggled ? '#007bff' : '#ffffff';
    });
  }

  // 初始化函数
  function initialize() {
    initializeData();
    const sidebar = createSidebar();
    document.body.appendChild(sidebar);
  }
})();